using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Security;
using BizWeb.Core.Extensions;
using BizWeb.Core.Helpers;
using BizWeb.Models;
using BizWeb.Models.Repository;
using BizWeb.Models.Interface;

namespace BizWeb.Controllers
{
    public class PostsController : Controller
    {
        // Setup Repository
        PostRepository postRepository = new PostRepository();
        private readonly IRegularExpressions expressions;
        
        // Support Injection
        //public PostsController() : this(new PostRepository()) { }
        //public PostsController(IPost repository)
        //{
        //    postRepository = repository;
        //}

        // GET: /Posts/
        public ActionResult Index(int? page)
        {           
            var posts = postRepository.AllPosts(false);
            var paginatedPosts = new PaginatedList<Site_Post>(posts, page ??0, 2);

            return View(paginatedPosts);
        }

        // GET: /Posts/2009/04/18/My_Slug_Here
        public ActionResult Details(int year, int month, int day, String slug)
        {
            var post = postRepository.GetPost(DateTime.Parse(month + "/" + day + "/" + year), slug);

            if (post == null)
                return View("NotFound");
            else
                return View(post);
        }

        // GET: /Posts/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        // POST: /Posts/Create
        [AcceptVerbs(HttpVerbs.Post), Authorize, ValidateInput(false), ValidateAntiForgeryToken]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // Establish new Post
                Site_Post post = new Site_Post();

                // Update values
                DateTime now = DateTime.Now;
                post.PostID = Guid.NewGuid();
                post.Title = Request.Form["Title"];
                post.Slug = expressions.GenerateSlug(Request.Form["Title"]);
                post.RichContent = Request.Form["RichContent"];
                post.PlainContent = expressions.StripHtml(Request.Form["RichContent"]);
                // TODO: Determine why the checkbox is constantly returning true and false
                post.IsPublished = true; // Boolean.Parse(Request.Form["IsPublished"]);
                post.DateCreated = now;
                // TODO: Change the way the UserID is handled. Just because the SqlProvider for Membership handles it as a Guid doesn't mean all providers do. (Jason Gaylord)
                post.UserId = (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey;
                // TODO: The Friendly Author field was added to provide a quick reference to the author's name. This field should be eliminated or should pull from the user's profile going forward (Jason Gaylord)
                post.FriendlyAuthor = User.Identity.Name;
                if (post.IsPublished)
                    post.DatePublished = now;
                else
                    post.DatePublished = null;

                // Save values
                postRepository.Add(post);
                postRepository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: /Posts/Edit/{Guid}
        [Authorize]
        public ActionResult Edit(Guid id)
        {
            var post = postRepository.GetPost(id);

            if (post == null)
                return View("NotFound");
            else
                return View(post);
        }


        // POST: /Posts/Edit/{Guid}
        [AcceptVerbs(HttpVerbs.Post), Authorize, ValidateInput(false), ValidateAntiForgeryToken]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            try
            {
                // Grab existing post
                Site_Post post = postRepository.GetPost(id);
 
                // Update values
                bool originalPublishedValue = post.IsPublished;
                DateTime now = DateTime.Now;
                post.Title = Request.Form["Title"];
                post.RichContent = Request.Form["RichContent"];
                post.PlainContent = expressions.StripHtml(Request.Form["RichContent"]);
                // TODO: Determine why the checkbox is constantly returning true and false
                post.IsPublished = true; // Boolean.Parse(Request.Form["IsPublished"]);
                post.DateUpdated = now;
                if (post.IsPublished)
                {
                    if (post.IsPublished != originalPublishedValue)
                        post.DatePublished = now;
                }
                else {post.DatePublished = null;}

                // Save values
                postRepository.Save();

                // Return to index
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: /Posts/Delete/{Guid}
        [Authorize]
        public ActionResult Delete(Guid id)
        {
            var post = postRepository.GetPost(id);

            if (post == null)
                return View("NotFound");
            else
                return View(post);
        }

        // POST: /Posts/Delete/{Guid}
        [AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken] 
        public ActionResult Delete(Guid id, string deleteButton)
        {
            Site_Post post = postRepository.GetPost(id);

            if (post == null)
                return View("NotFound");

            postRepository.Delete(post);
            postRepository.Save();

            return View("Deleted");
        }
    }
}